package br.ufmg.dcc.simulesspl.model;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class GameBoardTest {

	@Test
	public void addingSprints() {
		Sprint s1 = new Sprint();
		Sprint s2 = new Sprint();

		GameBoard gameBoard = new GameBoard();
		
		gameBoard.setCurrentSprint(s1);
		assertThat(gameBoard.getPastSprints().size(), equalTo(0));
		assertThat(gameBoard.getCurrentSprint(), equalTo(s1));
		
		gameBoard.applySprint();
		assertThat(gameBoard.getPastSprints().size(), equalTo(1));
		assertThat(gameBoard.getCurrentSprint(), equalTo(null));
		
		gameBoard.setCurrentSprint(s2);
		gameBoard.applySprint();
		assertThat(gameBoard.getPastSprints().size(), equalTo(2));
		assertThat(gameBoard.getPastSprints().get(0), equalTo(s1));
		assertThat(gameBoard.getPastSprints().get(1), equalTo(s2));
		assertThat(gameBoard.getCurrentSprint(), equalTo(null));
	}

	@Test
	public void totalCodeOrBugsIsEqualToSumOfSpintCodeOrBugs() {
		GameBoard gameBoard = new GameBoard();

		Sprint s1 = new Sprint() {
			@Override
			public int getBugs() {
				return 2;
			}

			@Override
			public int getCode() {
				return 3;
			}
		};
		gameBoard.setCurrentSprint(s1);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalCode(), equalTo(s1.getCode()));
		assertThat(gameBoard.getTotalBugs(), equalTo(s1.getBugs()));

		Sprint s2 = new Sprint() {
			@Override
			public int getBugs() {
				return 7;
			}

			@Override
			public int getCode() {
				return 18;
			}
		};
		gameBoard.setCurrentSprint(s2);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalCode(), equalTo(s1.getCode() + s2.getCode()));
		assertThat(gameBoard.getTotalBugs(), equalTo(s1.getBugs() + s2.getBugs()));
	}
	

	@Test
	public void gameIsOverWhenAllCodeIsCompleteAndThereIsNoBug() {
		GameBoard gameBoard = new GameBoard();

		ProjectCard project = new ProjectCard(1, 5);
		gameBoard.setProject(project);

		Sprint sprintWith5codeAnd0Bugs = new Sprint() {
			@Override
			public int getBugs() {
				return 0;
			}

			@Override
			public int getCode() {
				return 5;
			}

		};

		gameBoard.setCurrentSprint(sprintWith5codeAnd0Bugs);
		gameBoard.applySprint();

		assertThat(gameBoard.isOver(), equalTo(true));
	}

	@Test
	public void gameIsNotOverWhenThereAreBugs() {
		GameBoard gameBoard = new GameBoard();

		ProjectCard project = new ProjectCard(1, 5);
		gameBoard.setProject(project);

		Sprint sprintWith5codeAnd1Bug = new Sprint() {
			@Override
			public int getBugs() {
				return 1;
			}

			@Override
			public int getCode() {
				return 5;
			}

		};

		gameBoard.setCurrentSprint(sprintWith5codeAnd1Bug);
		gameBoard.applySprint();

		assertThat(gameBoard.isOver(), equalTo(false));
	}
	
	@Test
	public void bugFixesShouldDecrementTheNumberOfBugsUntil0AtMost() {
		Sprint sprintWithOneBugFix = new Sprint() {
			@Override
			public int getBugFixes() {
				return 1;
			}
		};
		
		Sprint sprintWithTwoBugsAndTwoCodes = new Sprint() {
			@Override
			public int getBugs() {
				return 2;
			}
			@Override
			public int getCode() {
				return 2;
			}
		};
		
		GameBoard gameBoard = new GameBoard();
		
		gameBoard.setCurrentSprint(sprintWithTwoBugsAndTwoCodes);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalCode(), equalTo(2));
		assertThat(gameBoard.getTotalBugs(), equalTo(2));
		
		gameBoard.setCurrentSprint(sprintWithOneBugFix);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalBugs(), equalTo(1));
		
		gameBoard.setCurrentSprint(sprintWithOneBugFix);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalBugs(), equalTo(0));
		
		gameBoard.setCurrentSprint(sprintWithOneBugFix);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalBugs(), equalTo(0));
		
		gameBoard.setCurrentSprint(sprintWithOneBugFix);
		gameBoard.applySprint();
		assertThat(gameBoard.getTotalBugs(), equalTo(0));
		
	}
}
